package learningThread.syn.synchronizedMethodLockObj.synchronizedOther;

import java.util.ArrayList;
import java.util.List;

public class OperationObj {



    private List<Integer> list = new ArrayList();


    /**
     * 使用局部锁
     * @param a
     */
    public void method(Integer a){
        try {
            System.out.println("method begin synchronized(this)操作对象list");
            for (int i = 0; i < 10; i++) {
                System.out.println(" thread name:"+Thread.currentThread().getName()+",i=  "+i );
            }
            synchronized (this){
                list.add(a);
                Thread.sleep(2000);
                System.out.println(" end synchronized(this)操作对象list， thread name:"+Thread.currentThread().getName() );
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 不同的锁对象，可以进行异步调用。
     * @param a
     */
    public void methodOther(Integer a){
        System.out.println("method begin methodOther(list)");
        //synchronized (list){
        synchronized (this){
            for (int i = 0; i < 10; i++) {
                System.out.println("我现在锁list对象，进行分析i:"+i);
            }
        }
    }
}
